package com.pwc.ackAdvance;

import com.pwc.utils.RabbitUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author fantuancyh
 * @data 2021/8/22
 */
@Component
@Slf4j
public class ConfirmExchangeCallback implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback{
    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct()
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
    }
    // 判断交换机是否收到消息
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if(ack){
            // 成功发送信息到交换机
            log.info("成功发送信息到交换机");
        }else{
            //发送信息到交换机失败
            log.info("发送信息到交换机失败");
        }
    }
    //消息不可达到队列时回退给生产层
    @Override
    public void returnedMessage(ReturnedMessage returned) {
        String s = new String(returned.getMessage().getBody());
        log.info("失败原因{}",s);
    }
}
